<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px;">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <!-- <a-col :span="6" >
              <a-form-item label="风险类型" prop="type">
                <a-select placeholder="请选择风险类型" v-model="queryParam.type" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in typeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :span="6" >
              <a-form-item label="关键词">
                <a-input v-model="queryParam.keywords" placeholder="请输入关键词" allow-clear @keyup.enter.native="handleQuery"/>
              </a-form-item>
            </a-col>
            <a-col :span="6" >
              <a-form-item label="状态" prop="status">
                <a-select placeholder="请选择状态" v-model="queryParam.status" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6" >
              <a-form-item label="特货代码">
                <a-input v-model="queryParam.specialCode" placeholder="请输入特货代码" allow-clear @keyup.enter.native="handleQuery"/>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <risk-goods-name-key-suggest-add-form
        v-if="showAddModal"
        ref="riskGoodsNameKeySuggestAddForm"
        :typeOptions="typeOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <risk-goods-name-key-suggest-edit-form
        v-if="showEditModal"
        ref="riskGoodsNameKeySuggestEditForm"
        :typeOptions="typeOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <advance-table
        title="疑似风险优化建议"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
        tableKey="base-riskGoodsNameKeySuggest-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="riskGoodsNameKeySuggestList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <div class="table-operations" slot="button">
          <!-- <a-button type="primary" @click="handleAdd">
            <a-icon type="plus" />新增
          </a-button> -->
          <!-- <a-button type="primary" :disabled="multiple" @click="handleCheck">
            <a-icon type="check" />审核
          </a-button> -->
          <a-dropdown>
            <a-menu slot="overlay" @click="handleCheck">
              <a-menu-item :key=1> <a-icon type="check" />通过</a-menu-item>
              <a-menu-item :key=2> <a-icon type="close" />不通过</a-menu-item>
            </a-menu>
            <a-button type="primary" :disabled="multiple" v-hasPermi="['riskGoodsNameKeySuggestApply']">
              <a-icon type="check" />审核
            </a-button>
          </a-dropdown>
          <a-dropdown>
            <a-menu slot="overlay" @click="handleConfirm">
              <a-menu-item :key=3> <a-icon type="check" />通过</a-menu-item>
              <a-menu-item :key=4> <a-icon type="close" />不通过</a-menu-item>
            </a-menu>
            <a-button type="primary" :disabled="multiple" v-hasPermi="['riskGoodsNameKeySuggestconfirm']">
              <a-icon type="check" />确认
            </a-button>
          </a-dropdown>
          <a-button type="" @click="handleExport">
            <a-icon type="download" />导出
          </a-button>
          <a-button type="danger" v-if="!multiple" :disabled="multiple" @click="handleDelete">
            <a-icon type="delete" />删除
          </a-button>
        </div>
        <span slot="type"  slot-scope="{record}">
          {{ typeFormat(record) }}
        </span>
        <span slot="status"  slot-scope="{record}">
          {{ statusFormat(record) }}
        </span>
        <span slot="operation"  slot-scope="{text, record}">
          <template v-if="record.status == 0">
            <a-dropdown>
              <a-menu slot="overlay" @click="(e) => handleCheck(e, record.id)">
                <a-menu-item :key=1> <a-icon type="check" />通过</a-menu-item>
                <a-menu-item :key=2> <a-icon type="close" />不通过</a-menu-item>
              </a-menu>
              <a v-hasPermi="['riskGoodsNameKeySuggestApply']">
                审核
              </a>
            </a-dropdown>
            <a-divider v-hasPermi="['riskGoodsNameKeySuggestApply']" type="vertical"/>
          </template>
          <template v-if="record.status == 1">
             <a-dropdown>
              <a-menu slot="overlay" @click="(e) => handleConfirm(e, record.id)">
                <a-menu-item :key=3> <a-icon type="check" />通过</a-menu-item>
                <a-menu-item :key=4> <a-icon type="close" />不通过</a-menu-item>
              </a-menu>
              <a v-hasPermi="['riskGoodsNameKeySuggestconfirm']">
                确认
              </a>
            </a-dropdown>
            <a-divider v-hasPermi="['riskGoodsNameKeySuggestconfirm']" type="vertical"/>
          </template>
          <a @click="handleUpdate(record)">
            修改
          </a>
          <a-divider type="vertical"/>
          <a @click="handleDelete(record)">
            删除
          </a>
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import { listRiskGoodsNameKeySuggest, delRiskGoodsNameKeySuggest, exportRiskGoodsNameKeySuggest, apply, confirm } from '@/api/system/riskGoodsNameKeySuggest'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import RiskGoodsNameKeySuggestAddForm from '@/views/system/riskgoodsnamekeysuggest/modules/RiskGoodsNameKeySuggestAddForm'
import RiskGoodsNameKeySuggestEditForm from '@/views/system/riskgoodsnamekeysuggest/modules/RiskGoodsNameKeySuggestEditForm'
export default {
  name: 'RiskGoodsNameKeySuggest',
  components: {
    AdvanceTable,
    RiskGoodsNameKeySuggestAddForm,
    RiskGoodsNameKeySuggestEditForm
  },
  data () {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 优化建议信息表格数据
      riskGoodsNameKeySuggestList: [],
      // 风险类型字典
      typeOptions: [],
      // 状态 0 未审核 1 审核通过 2 审核拒绝字典
      statusOptions: [
        { dictLabel: '未审核', dictValue: '0' },
        { dictLabel: '审核通过', dictValue: '1' },
        { dictLabel: '审核拒绝', dictValue: '2' },
        { dictLabel: '确认通过', dictValue: '3' },
        { dictLabel: '确认不通过', dictValue: '4' }
      ],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        keywords: undefined,
        specialCode: undefined,
        status: undefined
      },
      columns: [
        {
          title: '关键词',
          dataIndex: 'keywords',
          ellipsis: true,
          align: 'left',
          width: '20%'
        },
        {
          title: '特货代码',
          dataIndex: 'specialCode',
          ellipsis: true,
          align: 'left',
          width: '20%'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          align: 'center',
          width: '20%'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '20%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created () {
    this.getList()
    this.getDicts('cargo_list_type').then(response => {
      this.typeOptions = response.data
    })
    // this.getDicts('cargo_detail_status').then(response => {
    //   this.statusOptions = response.data
    // })
  },
  methods: {
    /** 查询优化建议信息列表 */
    getList () {
      this.loading = true
      this.queryParam.type = 2
      listRiskGoodsNameKeySuggest(this.queryParam).then(response => {
        this.riskGoodsNameKeySuggestList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    // 风险类型字典翻译
    // 1 X光机难以辨识 2 疑似风险 3 含隐含性危险品字典翻译
    typeFormat (row) {
        if (row.type || row.type === 0) {
            return this.selectDictLabel(this.typeOptions, row.type + '')
        } else {
            return ''
        }
    },
    // 状态 0 未审核 1 审核通过 2 审核拒绝字典翻译
    statusFormat (row) {
        if (row.status || row.status === 0) {
            return this.selectDictLabel(this.statusOptions, row.status + '')
        } else {
            return ''
        }
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        keywords: undefined,
        specialCode: undefined,
        status: undefined
      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange (current, size) {
        this.queryParam.pageNum = 1
        this.queryParam.pageSize = size
        this.getList()
    },
    /** 翻页操作 */
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
        this.ids = this.selectedRows.map(item => item.id)
        this.single = selectedRowKeys.length !== 1
        this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced () {
        this.advanced = !this.advanced
    },
    handleAdd () {
          this.showAddModal = true
          this.$nextTick(() => (
                  this.$refs.riskGoodsNameKeySuggestAddForm.handleAdd()
          ))
    },
    handleUpdate (record, ids) {
          this.showEditModal = true
          this.$nextTick(() => (
                  this.$refs.riskGoodsNameKeySuggestEditForm.handleUpdate(record, ids)
          ))
    },
    handleCheck (e, id) {
      let ids = id ? [id] : this.ids;
      var that = this
      this.$confirm({
        title: '审核',
        content: '请确认您的操作',
        okText: '确认',
        cancelType: 'danger',
        cancelText: '取消',
        onOk () {
          return apply({
            status: e.key,
            ids
          }).then(res => {
            that.onSelectChange([], [])
            that.getList()
          })
        }
        // onCancel () {
        //    return apply({
        //     status: 2,
        //     ids: that.ids
        //   }).then(res => {
        //     that.onSelectChange([], [])
        //     that.getList()
        //   })
        // }
      })
    },
    handleConfirm (e, id) {
      let ids = id ? [id] : this.ids;
      var that = this
      this.$confirm({
        title: '确认',
        content: '请确认您的操作',
        okText: '确认',
        cancelType: 'danger',
        cancelText: '取消',
        onOk () {
          return confirm({
            status: e.key,
            ids
          }).then(res => {
            that.onSelectChange([], [])
            that.getList()
          })
        }
        // onCancel () {
        //    return apply({
        //     status: 2,
        //     ids: that.ids
        //   }).then(res => {
        //     that.onSelectChange([], [])
        //     that.getList()
        //   })
        // }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const riskGoodsNameKeySuggestIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk () {
          return delRiskGoodsNameKeySuggest(riskGoodsNameKeySuggestIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          return exportRiskGoodsNameKeySuggest(that.queryParam)
            .then(response => {
              that.download(response.msg)
              that.$message.success(
                '导出成功',
                3
              )
          })
        },
        onCancel () {}
      })
    }
  }
}
</script>
